/*
 * scheduler.h
 *
 *  Created on: Aug 10, 2010
 *      Author: sdaniele
 */

#ifndef SCHEDULER_H_
#define SCHEDULER_H_


#include "queue.h"
#include "event.h"
#include "event_registry.h"

#include <boost/utility.hpp>

#include "tbb/task.h"
#include "tbb/tick_count.h"
#include "tbb/mutex.h"
#include "tbb/concurrent_vector.h"
#include "tbb/atomic.h"

#include <assert.h>

#include <vector>
#include <iostream>
#include <string>

namespace pilot {
class MyMethod;

typedef ::tbb::mutex mutex_t;

class scheduler
	: private boost::noncopyable
{


public:
	enum phase { before_start = 0, running, stopped };

	scheduler();
	virtual ~scheduler();


	void start();
	void stop();

	void spawn(process*);
	void submit(process*);

	void register_event(event*);


	double seconds_elapsed() const;
	bool is_par_context() const {
		return m_is_parcontext;
	}

public: /*internal*/
	void spawn_group_internal(const event::container_type& group);


private:
	void cycle();

	bool should_continue() const;

	unsigned long 					m_delta;
	phase 							m_phase;
	bool							m_is_parcontext;
	tbb::tick_count::interval_t  	m_elapsed;
	tbb::tick_count					m_started_at;

	tbb::atomic<bool> 				m_should_stop;
	queue_t							m_process_queue;
	event_registry_t				m_event_registry;
};

}

#endif /* SCHEDULER_H_ */
